/**
 *  This software is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Use this application at your own risk.
 *
 *  Copyright (c) 2011 by Orfeo Morello.
 */
package com.localstreamer.mobile;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
 
/**
 * Browse the localstreamer service
 * 
 * @author Orfeo Morello
 *
 */
public class RemoteBrowser extends Activity {
    
   public WebView mWebView;
   
   public String customUrl;
   
   public String aboutUrl="http://www.localstreamer.com/pages/mobile/about.html";
   
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
       
      // Adds Progrss bar Support
      this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
      this.setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
      
      setContentView(R.layout.webview );
       
      // Makes Progress bar Visible
      getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
 
       // Get Web view
       mWebView = (WebView) findViewById( R.id.webview ); //This is the id you gave
                                              //to the WebView in the webview.xml
       
       mWebView.setInitialScale(130);
       
       mWebView.getSettings().setJavaScriptEnabled(true);  
       mWebView.getSettings().setSupportZoom(true);       //Zoom Control on web (You don't need this
                                              //if ROM supports Multi-Touch     
       mWebView.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM
       
       mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
       mWebView.getSettings().setSavePassword(true);
       mWebView.getSettings().setSaveFormData(true);
       mWebView.getSettings().setSupportMultipleWindows(false);
       mWebView.setVerticalScrollBarEnabled(true);

       mWebView.clearHistory();
       mWebView.clearFormData();
       mWebView.clearCache(true);
       mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

       // get actual screen size
       /*
       Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
       int width = display.getWidth(); 
       int height = display.getHeight(); 
       */

       Bundle b = getIntent().getExtras();

       customUrl = b.getString("customUrl");
       final ProgressDialog dialog = ProgressDialog.show(RemoteBrowser.this, "",  "Loading", true);

       // Load URL
       mWebView.loadUrl(customUrl);
       mWebView.setWebViewClient(new WebViewClient() {

    		@Override
    		public boolean shouldOverrideUrlLoading(WebView view, String url) {
    			view.loadUrl(url);
    			return true;
    		}

    		public void onPageFinished(WebView view, String url) 
    	    {
    	        dialog.dismiss();
    	    }
       }
    		);        
       mWebView.setWebChromeClient(new WebChromeClient() {
           public void onProgressChanged(WebView view, int progress) {
        	   RemoteBrowser.this.setProgress(progress * 100);
           }
           
           

       });
        
   }//End of Method onCreate
   
   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event) {
	   if (keyCode == KeyEvent.KEYCODE_BACK&& mWebView.canGoBack()) 
	   {       
           if(!mWebView.getUrl().equals(customUrl))
           {
        	mWebView.goBack();
           	return true;
           }
       }
	   return super.onKeyDown(keyCode, event);
   }
   
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 1001, 0, "Home"); 
		menu.add(0, 1002, 0, "About");
		menu.add(0, 1003, 0, "Quit");
		return super.onCreateOptionsMenu(menu); 
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		switch (item.getItemId()) 
		{
			case 1003:
			{
			this.finish();
			android.os.Process.killProcess(android.os.Process.myPid());		    	          
			break;
			}
			case 1002:
			{
				mWebView.loadUrl(aboutUrl);
				break;
			}
			case 1001:
			{
				mWebView.loadUrl(customUrl);
				break;
			}
		}
		return super.onOptionsItemSelected(item);
	}
	
	
}